package com.njbdqn.banks.introller;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.njbdqn.banks.bizmodule.UserinfosService;
import com.njbdqn.banks.vomodule.Traninfos;
import com.njbdqn.banks.vomodule.Userinfos;

@Controller
@CrossOrigin(origins = "*")
public class InitCtrl {
	@Autowired
	private UserinfosService userinfosService;
	
	@InitBinder
	public void dataFormat(WebDataBinder wdb){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		sdf.setLenient(true);
		wdb.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
	}
	
	@RequestMapping("/init.do")
	public String init(Userinfos user,@RequestParam(required=false,defaultValue="1")int cp,Model model ){
		model.addAttribute("all",userinfosService.sameQuery(cp, user));
		return "index.jsp";
	}
	@RequestMapping("/ajax.do")
	@ResponseBody
	public List<Traninfos> searchTran(int userid){
		return userinfosService.findUserTran(userid);
	}
	
	@RequestMapping("/save.do")
	public String save(@RequestParam("file")MultipartFile file, Userinfos user,Traninfos tran,HttpSession session){
		String path = session.getServletContext().getRealPath("/img");
        File fpath = new File(path);
        if (!fpath.exists()) {
            fpath.mkdirs();
        }

        String oldName = file.getOriginalFilename();
        File upfile = new File(path, oldName);
       
            try {
				file.transferTo(upfile);
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

        user.setUserphoto(oldName);;
        userinfosService.openAccount(user, tran);
		return "redirect:/init.do";
	}
	
	@RequestMapping("del.do")
	public String del(int userid){
		return "redirect:/init.do";
	}
	
	@RequestMapping
	public String update(Userinfos user){
		userinfosService.updateUser(user);
		return "redirect:/init.do";
	}
	@RequestMapping("/single.do")
    public String selectOne(int userid,Model model){
        model.addAttribute("one",userinfosService.findById(userid));
        return "update.jsp";
    }
	
}
